﻿namespace WMS6.Web.CustomizeReport
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CustomizeReportServiceRef;
    using WMS6.Web.UserControls;

    public class YITAISerialNoInOutRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F16051;
        protected GridView gvSale;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblSerialNoInOutRptList;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        public void BindData2StockList(YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(serialNoInOutReportQueryEntity.CurrentPage);
            DataSet ds = CustomizeReportServiceClientProxy.GetInstance().GetYITAISerialNoInOutReport(serialNoInOutReportQueryEntity);
            base.BindData2GridView(this.gvSale, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity = this.GetLPSaleReportQueryEntity();
            serialNoInOutReportQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = serialNoInOutReportQueryEntity.CurrentPage;
            serialNoInOutReportQueryEntity.CurrentPage = "0";
            this.gvSale.Columns[1].Visible = false;
            this.gvSale.AllowSorting = false;
            this.gvSale.ShowFooter = false;
            List<string> transIDs = new List<string>();
            List<string> otTransIDs = new List<string> { "", "" };
            try
            {
                DataSet ds = CustomizeReportServiceClientProxy.GetInstance().GetYITAISerialNoInOutReport(serialNoInOutReportQueryEntity);
                StringBuilder sb = new StringBuilder();
                if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
                {
                    string line = "";
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        string receiptDate = string.IsNullOrEmpty(ds.Tables[0].Rows[i]["RECEIPT_DATE"].ToString()) ? "" : DateTime.Parse(ds.Tables[0].Rows[i]["RECEIPT_DATE"].ToString()).ToString("yyyy-M-d");
                        string shipDate = string.IsNullOrEmpty(ds.Tables[0].Rows[i]["ACTUAL_SHIP_DATE"].ToString()) ? "" : DateTime.Parse(ds.Tables[0].Rows[i]["ACTUAL_SHIP_DATE"].ToString()).ToString("yyyy-M-d");
                        line = ds.Tables[0].Rows[i]["SKU_CLASS_ID"].ToString() + "\t" + ds.Tables[0].Rows[i]["SKU_ID"].ToString() + "\t" + ds.Tables[0].Rows[i]["SKU_DESCR2"].ToString() + "\t" + ds.Tables[0].Rows[i]["SKU_DESCR"].ToString() + "\t" + ds.Tables[0].Rows[i]["LOT_ATTR05"].ToString() + "\t\t" + ds.Tables[0].Rows[i]["RECEIPT_TYPE"].ToString() + "\t" + ds.Tables[0].Rows[i]["EXTERNAL_RECEIPT_ID"].ToString() + "\t" + ds.Tables[0].Rows[i]["VENDOR_ID"].ToString() + "\t" + ds.Tables[0].Rows[i]["VENDOR_NAME"].ToString() + "\t" + receiptDate + "\t" + receiptDate + "\t" + ds.Tables[0].Rows[i]["ORDER_TYPE"].ToString() + "\t" + ds.Tables[0].Rows[i]["EXTERN_ORDER_ID"].ToString() + "\t" + ds.Tables[0].Rows[i]["CUST_ID"].ToString() + "\t" + ds.Tables[0].Rows[i]["CUST_NAME"].ToString() + "\t" + shipDate + "\t" + ds.Tables[0].Rows[i]["WH_PROPERTY"].ToString() + "\t" + ds.Tables[0].Rows[i]["FLAG"].ToString() + "\t\t";
                        sb.AppendLine(line);
                        if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["TRANS_ID"].ToString().Trim()))
                        {
                            transIDs.Add(ds.Tables[0].Rows[i]["TRANS_ID"].ToString().Trim());
                        }
                        if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["OT_TRANS_ID"].ToString().Trim()))
                        {
                            otTransIDs.Add(ds.Tables[0].Rows[i]["OT_TRANS_ID"].ToString().Trim());
                        }
                    }
                    this.Page.Response.Clear();
                    this.Page.Response.Buffer = true;
                    this.Page.Response.Charset = "GB2312";
                    this.Page.Response.AppendHeader("Content-Disposition", "attachment;filename=abc.txt");
                    this.Page.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
                    this.Page.Response.ContentType = "text/plain";
                    this.Page.Response.Write(sb.ToString());
                    this.Page.Response.End();
                }
            }
            finally
            {
                CustomizeReportServiceClientProxy.GetInstance().UpdateYITAISerialNoInOutReportFlag(transIDs, otTransIDs);
                serialNoInOutReportQueryEntity.IsGetAll = false;
                serialNoInOutReportQueryEntity.CurrentPage = currentPageIndex;
                this.SetSerialNoInOutReportQueryEntity(serialNoInOutReportQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity = this.GetLPSaleReportQueryEntity();
            serialNoInOutReportQueryEntity.CurrentPage = "0";
            this.BindData2StockList(serialNoInOutReportQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity = this.GetLPSaleReportQueryEntity();
            serialNoInOutReportQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2StockList(serialNoInOutReportQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity = this.GetLPSaleReportQueryEntity();
            serialNoInOutReportQueryEntity.OWNER_ID = "";
            serialNoInOutReportQueryEntity.SKU_ID = "";
            serialNoInOutReportQueryEntity.EXTERNAL_RECEIPT_ID = "";
            serialNoInOutReportQueryEntity.VENDOR_ID = "";
            serialNoInOutReportQueryEntity.BEGIN_RECEIPT_DATE = "";
            serialNoInOutReportQueryEntity.END_RECEIPT_DATE = "";
            serialNoInOutReportQueryEntity.EXTERNAL_ORDER_ID = "";
            serialNoInOutReportQueryEntity.CUST_ID = "";
            serialNoInOutReportQueryEntity.BEGIN_SHIP_DATE = "";
            serialNoInOutReportQueryEntity.END_SHIP_DATE = "";
            this.BindData2StockList(serialNoInOutReportQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity = this.GetLPSaleReportQueryEntity();
            if (int.Parse(serialNoInOutReportQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                serialNoInOutReportQueryEntity.CurrentPage = (int.Parse(serialNoInOutReportQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2StockList(serialNoInOutReportQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity = this.GetLPSaleReportQueryEntity();
            if (int.Parse(serialNoInOutReportQueryEntity.CurrentPage) > 0)
            {
                serialNoInOutReportQueryEntity.CurrentPage = (int.Parse(serialNoInOutReportQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2StockList(serialNoInOutReportQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity = this.GetLPSaleReportQueryEntity();
            this.BindData2StockList(serialNoInOutReportQueryEntity);
        }

        private YITAISerialNoInOutReportQueryEntity GetLPSaleReportQueryEntity()
        {
            return (base.PageQueryEntitySession as YITAISerialNoInOutReportQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity = this.GetLPSaleReportQueryEntity();
                serialNoInOutReportQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2StockList(serialNoInOutReportQueryEntity);
            }
        }

        protected void gvSale_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F16051", this.gvSale);
        }

        protected void gvSale_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (this.hdnActionID.Value != "EXPORT")
                {
                    e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                    e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                    ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                    e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
            }
        }

        protected void gvSale_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity = this.GetLPSaleReportQueryEntity();
                serialNoInOutReportQueryEntity.SortField = e.SortExpression;
                if ((serialNoInOutReportQueryEntity.SortDirection == "ASC") || (serialNoInOutReportQueryEntity.SortDirection == ""))
                {
                    serialNoInOutReportQueryEntity.SortDirection = "DESC";
                }
                else if (serialNoInOutReportQueryEntity.SortDirection == "DESC")
                {
                    serialNoInOutReportQueryEntity.SortDirection = "ASC";
                }
                this.BindData2StockList(serialNoInOutReportQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity = new YITAISerialNoInOutReportQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    serialNoInOutReportQueryEntity.OWNER_ID = this.PreviousPage.OwnerID;
                    serialNoInOutReportQueryEntity.SKU_ID = this.PreviousPage.SkuID;
                    serialNoInOutReportQueryEntity.EXTERNAL_RECEIPT_ID = this.PreviousPage.ExternalReceiptID;
                    serialNoInOutReportQueryEntity.VENDOR_ID = this.PreviousPage.VendorID;
                    serialNoInOutReportQueryEntity.BEGIN_RECEIPT_DATE = this.PreviousPage.BeginReceiptDate;
                    serialNoInOutReportQueryEntity.END_RECEIPT_DATE = this.PreviousPage.EndReceiptDate;
                    serialNoInOutReportQueryEntity.EXTERNAL_ORDER_ID = this.PreviousPage.ExternalOrderID;
                    serialNoInOutReportQueryEntity.CUST_ID = this.PreviousPage.CustID;
                    serialNoInOutReportQueryEntity.BEGIN_SHIP_DATE = this.PreviousPage.BeginShipDate;
                    serialNoInOutReportQueryEntity.END_SHIP_DATE = this.PreviousPage.EndShipDate;
                }
                else
                {
                    serialNoInOutReportQueryEntity.OWNER_ID = "";
                    serialNoInOutReportQueryEntity.SKU_ID = "";
                    serialNoInOutReportQueryEntity.EXTERNAL_RECEIPT_ID = "";
                    serialNoInOutReportQueryEntity.VENDOR_ID = "";
                    serialNoInOutReportQueryEntity.BEGIN_RECEIPT_DATE = "";
                    serialNoInOutReportQueryEntity.END_RECEIPT_DATE = "";
                    serialNoInOutReportQueryEntity.EXTERNAL_ORDER_ID = "";
                    serialNoInOutReportQueryEntity.CUST_ID = "";
                    serialNoInOutReportQueryEntity.BEGIN_SHIP_DATE = "";
                    serialNoInOutReportQueryEntity.END_SHIP_DATE = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            serialNoInOutReportQueryEntity.IsGetAll = false;
            serialNoInOutReportQueryEntity.CurrentPage = "0";
            serialNoInOutReportQueryEntity.PageSize = this.gvSale.PageSize.ToString();
            serialNoInOutReportQueryEntity.SortField = "LOT_ATTR10, RECEIPT_LINE_ID, ROWID";
            serialNoInOutReportQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvSale.PageSize;
            this.SetSerialNoInOutReportQueryEntity(serialNoInOutReportQueryEntity);
            this.BindData2StockList(serialNoInOutReportQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvSale);
                this.LoadData();
            }
        }

        public void SetSerialNoInOutReportQueryEntity(YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity)
        {
            base.PageQueryEntitySession = serialNoInOutReportQueryEntity;
        }

        public YITAISerialNoInOutRptQuery PreviousPage
        {
            get
            {
                return (YITAISerialNoInOutRptQuery) base.PreviousPage;
            }
        }
    }
}

